package com.dyzx.hbb.modules.wms.controller;


import com.dyzx.hbb.common.base.Result;
import com.dyzx.hbb.common.config.MyLambdaQueryWrapper;
import com.dyzx.hbb.common.controller.BaseValidController;
import com.dyzx.hbb.modules.wms.entity.WmsStockOutDetails;
import com.dyzx.hbb.modules.wms.query.WmsStockOutDetailsQuery;
import com.dyzx.hbb.modules.wms.service.WmsStockOutDetailsService;
import com.dyzx.hbb.modules.wms.vo.WmsStockOutDetailsUVO;
import com.dyzx.hbb.modules.wms.vo.WmsStockOutDetailsVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 出库单明细管理控制器
 * <p>处理仓库管理系统中出库单明细的查询和管理业务逻辑
 * <p>主要职责：
 * <ul>
 * <li>提供出库单明细的基础CRUD操作</li>
 * <li>支持按出库单ID、产品ID、货位ID、区域ID、状态和仓库ID等条件筛选明细信息</li>
 * <li>展示出库单明细的详细信息，包括关联的仓库名称、产品名称、机构名称和操作人名称</li>
 * <li>提供按出库单ID查询明细列表的专用接口</li>
 * </ul>
 * <p>设计意图：为仓库管理模块提供出库单明细的数据管理接口，
 * 确保出库明细数据的完整性和可追踪性，支持出库业务的精细化管理
 * 
 * @author 开发团队
 * @version 1.0.0
 * @since 2023-05-30
 */
@Tag(name = "出库单明细管理", description = "出库单明细相关接口")
@RestController
@RequestMapping("/wms/stock/out/details")
public class WmsStockOutDetailsController extends BaseValidController<
        WmsStockOutDetailsService,
        WmsStockOutDetails,
        WmsStockOutDetailsQuery,
        WmsStockOutDetailsVO,
        WmsStockOutDetailsUVO
        > {

    @Autowired
    private WmsStockOutDetailsService service;

    @Override
    protected WmsStockOutDetailsService getBaseService() {
        return service;
    }

    @Override
    protected MyLambdaQueryWrapper<WmsStockOutDetails> queryWrapper(WmsStockOutDetailsQuery query) {
        MyLambdaQueryWrapper<WmsStockOutDetails> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.select(WmsStockOutDetails::getId, WmsStockOutDetails::getOrderId, WmsStockOutDetails::getProductId, WmsStockOutDetails::getLocationId, WmsStockOutDetails::getLocationAreaId, WmsStockOutDetails::getStatus, WmsStockOutDetails::getHouseId,  WmsStockOutDetails::getCreateDate, WmsStockOutDetails::getUpdateDate);
        queryWrapper.houseName(WmsStockOutDetails::getHouseId).as("house_name")
                .select(WmsStockOutDetails::getQuantity);
        queryWrapper.wmsProductName(WmsStockOutDetails::getProductId).as("product_name");
        queryWrapper.select(WmsStockOutDetails::getPrice).as("price");
        queryWrapper.orderByDesc(WmsStockOutDetails::getCreateDate);
        queryWrapper.organName(WmsStockOutDetails::getOrganId).as("organ_name");
        queryWrapper.userName(WmsStockOutDetails::getOperatorId).as("operator_name");
        if (query != null) {
            queryWrapper.eq(query.getOrder_id() != null, WmsStockOutDetails::getOrderId, query.getOrder_id())
                    .eq(query.getProduct_id() != null, WmsStockOutDetails::getProductId, query.getProduct_id())
                    .eq(query.getLocation_id() != null, WmsStockOutDetails::getLocationId, query.getLocation_id())
                    .eq(query.getLocation_area_id() != null, WmsStockOutDetails::getLocationAreaId, query.getLocation_area_id())
                    .eq(query.getStatus() != null, WmsStockOutDetails::getStatus, query.getStatus())
                    .eq(query.getHouse_id() != null, WmsStockOutDetails::getHouseId, query.getHouse_id());
        }
        return queryWrapper;
    }

    @Override
    protected MyLambdaQueryWrapper<WmsStockOutDetails> queryInfoWrapper() {
        MyLambdaQueryWrapper<WmsStockOutDetails> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.orderByDesc(WmsStockOutDetails::getCreateDate);
        return queryWrapper;
    }

    @Override
    protected WmsStockOutDetails addValid(WmsStockOutDetailsVO entity) {
        return null;
    }

    @Override
    protected WmsStockOutDetails editValid(WmsStockOutDetailsUVO entity) {
        return null;
    }

    /*
    * @Description: 根据出库单ID查询出库单明细
    * @param: order_id
    *
    * */
    @GetMapping("/details")
    public Result<List<Map<String, Object>>> details(Long order_id) {
        MyLambdaQueryWrapper<WmsStockOutDetails> queryWrapper =queryWrapper(null);
        queryWrapper.eq(WmsStockOutDetails::getOrderId, order_id);
        List<Map<String, Object>> list = service.listMaps(queryWrapper);
        return list != null ? Result.ok(list) : Result.error(404, "暂无数据");
    }
}